package com.tang.campus_market.controller;

import com.tang.campus_market.entity.Comment;
import com.tang.campus_market.entity.User;
import com.tang.campus_market.util.CommentData;
import com.tang.campus_market.util.CommentDiv;
import com.tang.campus_market.service.CommentService;
import com.tang.campus_market.util.Result;
import com.tang.campus_market.util.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/comment")
@CrossOrigin //跨域
public class CommentController {
    @Autowired
    private CommentService commentService;
    /**
     * 获取评论，具体操作看Service层
     */

    @RequestMapping(value = "/getComments", method = RequestMethod.GET)
    public Result getComment(@RequestParam(value = "goodsId", required = true) Integer goodsId,
                             @RequestParam(value = "rootId", required = false) Integer rootId,
                             @RequestParam(value = "pageId", required = false, defaultValue = "1") int pageId,
                             HttpSession session) {
        //goodsId用于查询商品底下的评论。rootId用于展开评论底下的评论
        //rootId可为null，rootId用于指代
        //        通过session获取用户信息
        User user = (User) session.getAttribute("user");

        //-----------请求连接检测：1.是否指定商品id（goodsId）
        if (goodsId == null) {
            return new Result(StatusCode.ERROR, "未指定商品", null);
        }
        //-----------请求连接检测：2.是否指定评论的页面，没有指定则设置为1（pageId）
        if (pageId <= 0) {
            pageId = 1;
        }
        //------------请求连接检测：3.是否指定根评论，根评论是用于子评论的查询（rootId）
        if (rootId == null) {
            //-----------为null表示获取指定商品的首页评论操作
            CommentData commentData = commentService.getHomeComments(goodsId, pageId, 10);
            if(user==null){
                commentData.setNickName("请登录");
                commentData.setPicture("defaultProfileImage.jpg");
            }else{
                commentData.setNickName(user.getNickName());
                commentData.setPicture(user.getPicture());
            }
            return new Result(StatusCode.OK, "查询成功", commentData);
        } else {
            //else即：rootId != null，表示展开评论。
            CommentDiv commentDiv = commentService.getSubComments(rootId, pageId, 10);
            return new Result(StatusCode.OK, "查询成功", commentDiv);
        }
    }

    /**
     * 写评论，具体操作看Service层
     */
    @RequestMapping(value = "/giveComment", method = RequestMethod.POST)
    public Result giveComment(@RequestBody Comment comment,HttpSession session) {
        //Comment包含goodsId，commentId，userId，content；
        // 根评论的rootId与commentId都是null
        // goodsId不能为空，不然评论无所指
//        用户id验证：
        User user = (User) session.getAttribute("user");


        if (comment == null) {
            return new Result(StatusCode.ERROR, "空数据提交", null);
        } else if (comment.getGoodsId() == null) {
            //如果两者同时为空：表示评论没有指向的对象；或者同时不为空，则表示既评论的评论又评论的商品，两者皆为非法
            return new Result(StatusCode.ERROR, "未明确指定评论对象", null);
        } else if (comment.getUserId() == null) {
            return new Result(StatusCode.ERROR, "请先登录", null);
        } else if (comment.getContent() == null || comment.getContent().trim().isEmpty()) {
            return new Result(StatusCode.ERROR, "评论不能为空", null);
        }else if(!comment.getUserId().equals(user.getId())){
            //非session对应的用户访问。
            return new Result(StatusCode.REMOTEERROR, "请求参数非法", null);
        }
        //
        if (commentService.insert(comment) > 0) {
            comment = commentService.selectById(comment.getId());
            return new Result(StatusCode.OK, "查询成功", comment);
        } else {
            return new Result(StatusCode.REMOTEERROR, "请求参数非法", null);
        }
    }

    /**
     * 暂未使用
     * 后端数据库中删除对应评论，不返回刷新后的数据了，前端自行删除。
     *
     */
    @RequestMapping(value = "/deleteCommentByUser", method = RequestMethod.GET)
    public Result deleteCommentByUser(@RequestParam(value = "id", required = true) Integer id) {
        if (id <= 0) {
            return new Result(StatusCode.ERROR, "参数不合法", null);
        }
        int deleteResult = commentService.deleteCommentByUser(id);
        if (deleteResult < 1) {
            return new Result(StatusCode.ERROR, "评论不存在", null);
        } else {
            return new Result(StatusCode.OK, "成功删除" + deleteResult + "条评论", null);
        }
    }

    /**
     * 暂未使用
     * 管理员禁用评论
     * @param id 留言的id
     * @return 是否成功
     */
    @RequestMapping(value = "/banComment", method = RequestMethod.GET)
    public Result banComment(@RequestParam(value = "id", required = true) Integer id) {
        if (id <= 0) {
            return new Result(StatusCode.ERROR, "参数不合法", null);
        }
        int banResult = commentService.banComment(id);
        if (banResult < 1) {
            return new Result(StatusCode.ERROR, "评论不存在", null);
        } else {
            return new Result(StatusCode.OK, "成功删除" + banResult + "条评论", null);
        }
    }


}
